﻿@model GolfLeague.EditModels.PlayerEditModel

<script src="@Url.Content("~/Content/scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#dialog').dialog({
            autoOpen: false,
            model: true
        });
        $('#NewPlayer_PhoneNumber').mask("(999) 999-9999");
        $('button').button();
        $('input:button').button();
        $('input:submit').button();
        $('input:reset').button();
        $('div#buttonlist a').hover(
		    function () { $(this).addClass('ui-state-hover'); },
		    function () { $(this).removeClass('ui-state-hover'); }
	    );
    });

    $(function () {
        $(".DeleteButton").click(function (e) {
            e.preventDefault();
            var recordToDelete = $(this).attr("id");
            recordToDelete = recordToDelete.replace("delete-", "");

            $("#dialog").dialog("option", "buttons", {
                "Yes": function () {
                    $(this).dialog("close");
                    if (recordToDelete != '') {
                        $.post("/Player/PlayerDelete", { "playerID": recordToDelete },
                        function (data) {
                            $('#row-' + recordToDelete).fadeOut('slow');
                        });
                    }
                },
                "No": function () {
                    $(this).dialog("close");
                    return false;
                }
            });
            $("#dialog").dialog("open");
        });
    });

    $(function () {
        $(".EditButton").click(function (e) {
            e.preventDefault();
            var recordToEdit = $(this).attr("id");
            recordToEdit = recordToEdit.replace("edit-", "");

            if (recordToEdit != '') {
                window.location = '/Player/PlayerEdit/' + recordToEdit;
            }
        });
    });

    $(function () {
        $(".CancelEditButton").click(function (e) {
            e.preventDefault();
            window.location = "/Player/";
        });
    });

</script>

@{
    ViewBag.Title = "Players";
}
@Html.ActionLink("Admin Home", "Admin", "Home") > Players<br /><br />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Add Player</legend>
    
    @Html.HiddenFor(model => model.NewPlayer.PlayerID)

    <div class="editor-label">
        @Html.LabelFor(model => model.NewPlayer.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.NewPlayer.FirstName)
        @Html.ValidationMessageFor(model => model.NewPlayer.FirstName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.NewPlayer.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.NewPlayer.LastName)
        @Html.ValidationMessageFor(model => model.NewPlayer.LastName)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.NewHandicap.Handicap)
    </div>
    <div class="editor-field">
        @if (Model.NewPlayer.PlayerID < 1)
        {
            @Html.EditorFor(model => model.NewHandicap.Handicap)
            @Html.ValidationMessageFor(model => model.NewHandicap.Handicap)
        }
        else
        {
            @Html.DisplayTextFor(model => model.NewHandicap.Handicap)
        }
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.NewPlayer.PhoneNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.NewPlayer.PhoneNumber)
        @Html.ValidationMessageFor(model => model.NewPlayer.PhoneNumber)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.NewPlayer.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.NewPlayer.Email)
        @Html.ValidationMessageFor(model => model.NewPlayer.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.NewPlayer.Sub)
    </div>
    <div class="editor-field">
        @Html.CheckBoxFor(model => model.NewPlayer.Sub)
        @Html.ValidationMessageFor(model => model.NewPlayer.Sub)
    </div>

    <p>
        @if (Model.NewPlayer.PlayerID < 1)
        {
            <input type="submit" value="Add" />
        }
        else
        {
            <input type="submit" value="Save" />
            <input type="reset" class="CancelEditButton" value="Cancel" />
        }
    </p>
    <div id="dialog" title="Confirm Delete">Are you sure you want to delete this player?</div>
</fieldset>
}

<fieldset>
    <legend>Players</legend>
    <br />
    <table>
        <tr>
            <th>
                Last Name
            </th>
            <th>
                First Name
            </th>
            <th>
                Phone Number
            </th>
            <th>
                Email
            </th>
            <th>
                Handicap
            </th>
            <th>
                Sub
            </th>
            <th></th>
        </tr>

@foreach (var item in Model.Players)
{
        <tr id="row-@item.PlayerID" @if (item.EndedOn != null) {@Html.Raw("style=\"font-style:italic;background-color: Silver\"")}>
            <td>
                @item.LastName
            </td>
            <td>
                @item.FirstName
            </td>
            <td>
                @item.PhoneNumber
            </td>
            <td>
                @item.Email
            </td>
            <td>
                @item.PlayerHandicaps.OrderBy(h => h.CreatedOn).Last().Handicap
            </td>
            <td>
                @if (item.Sub)
                {
                    <input type="checkbox" checked="checked" disabled="disabled" />
                }
                else
                {
                    <input type="checkbox" disabled="disabled" />
                }
            </td>
            <td>
                <div id="buttonlist" style="vertical-align:middle">
                    <a href="#" title="Edit Player" class="EditButton ui-state-default ui-corner-all ui-icon ui-icon-pencil" id="edit-@item.PlayerID"></a>
                    <!--<a href="#" title="Delete Player" class="DeleteButton ui-state-default ui-corner-all ui-icon ui-icon-trash" id="delete-item.PlayerID"></a>-->
                </div>
                @if (item.EndedOn == null)
                {
                    <input type="button" class="DeactivateButton" id="deactivate-@item.PlayerID" value="Mark Inactive" />
                }
                else
                {
                    <input type="button" class="ActivateButton" id="activate-@item.PlayerID" value="Mark Active" />
                }
                    

            </td>
        </tr>
}
    </table>
</fieldset>
@Html.ActionLink("Back to Admin Home", "Admin", "Home")